package org.lex.ftplist.ui.util;

import java.util.HashMap;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import org.lex.ftplist.core.CacheService;
import org.lex.ftplist.core.Index;

public class IndexUtils {
	public static TreeNode getIndexTree() {
		DefaultMutableTreeNode root = new DefaultMutableTreeNode();
		// <host, tree_node>
		Map<String, DefaultMutableTreeNode> hostNodes = new HashMap<String, DefaultMutableTreeNode>();
		for (Index index : CacheService.getInstance().getIndexList()) {
			String host = index.getUrl().host;
			DefaultMutableTreeNode parent = hostNodes.get(host);
			if (null == parent) {
				parent = new DefaultMutableTreeNode(host);
				hostNodes.put(host, parent);
				root.add(parent);
			}
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(index);
			parent.add(node);
		}
		return root;
	}
}
